namespace QMVnSoft.SystemForms
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using QMVnSoft.DAL;
    using Microsoft.Office.Interop.Excel;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public class FormUpdateTaxCode : FormControl
    {
        private BtnControl btnClose;
        private BtnControl btnOk;
        private BtnControl btnPathBackup;
        private IContainer components;
        private LabelControl lblPathBackup;
        private TextControl txtRegisteredFile;

        public FormUpdateTaxCode()
        {
            this.InitializeComponent();
            this.Text = AppInstance.SoftwareName;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.UpdateTaxCode();
            base.Close();
        }

        private void btnPathBackup_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.InitialDirectory = "";
            dialog.Filter = "Tax code file (*.xls)|*.xls";
            dialog.CheckFileExists = true;
            dialog.ShowDialog();
            this.txtRegisteredFile.Text = dialog.FileName;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            //ComponentResourceManager manager = new ComponentResourceManager(typeof(FormUpdateTaxCode));
            this.btnOk = new BtnControl();
            this.btnClose = new BtnControl();
            this.btnPathBackup = new BtnControl();
            this.lblPathBackup = new LabelControl();
            this.txtRegisteredFile = new TextControl();
            base.SuspendLayout();
            this.btnOk.Font = new System.Drawing.Font("Arial", 8.25f);
            this.btnOk.Location = new System.Drawing.Point(0x21, 0x68);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new Size(100, 0x17);
            this.btnOk.TabIndex = 2;
            this.btnOk.Text = "&Chấp nhận";
            this.btnOk.UseVisualStyleBackColor = true;
            this.btnOk.Click += new EventHandler(this.btnOk_Click);
            //this.btnClose.DialogResult = DialogResult.Cancel;
            this.btnClose.Font = new System.Drawing.Font("Arial", 8.25f);
            this.btnClose.Location = new System.Drawing.Point(0xa6, 0x68);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new Size(100, 0x17);
            this.btnClose.TabIndex = 3;
            this.btnClose.Text = "&Quay ra";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new EventHandler(this.btnClose_Click);
            this.btnPathBackup.Font = new System.Drawing.Font("Arial", 8.25f);
            this.btnPathBackup.Location = new System.Drawing.Point(0xff, 0x19);
            this.btnPathBackup.Name = "btnPathBackup";
            this.btnPathBackup.Size = new Size(0x1d, 0x17);
            this.btnPathBackup.TabIndex = 1;
            this.btnPathBackup.TabStop = false;
            this.btnPathBackup.Text = "...";
            this.btnPathBackup.UseVisualStyleBackColor = true;
            this.btnPathBackup.Click += new EventHandler(this.btnPathBackup_Click);
            this.lblPathBackup.AutoSize = true;
            this.lblPathBackup.Font = new System.Drawing.Font("Arial", 8.25f);
            this.lblPathBackup.Location = new System.Drawing.Point(9, 0x1d);
            this.lblPathBackup.Name = "lblPathBackup";
            this.lblPathBackup.Size = new Size(0x63, 14);
            this.lblPathBackup.TabIndex = 6;
            this.lblPathBackup.Text = "Tên tệp mã số thuế";
            this.txtRegisteredFile.CheckTextSearched = false;
            this.txtRegisteredFile.DisabledForeColor = Color.Blue;
            this.txtRegisteredFile.Font = new System.Drawing.Font("Arial", 8.25f);
            this.txtRegisteredFile.Location = new System.Drawing.Point(12, 0x33);
            this.txtRegisteredFile.Name = "txtRegisteredFile";
            this.txtRegisteredFile.OldValue = null;
            this.txtRegisteredFile.Size = new Size(0x110, 20);
            this.txtRegisteredFile.TabIndex = 0;
//            this.txtRegisteredFile.Value = null;//manager.GetObject("txtRegisteredFile.Value");
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.CancelButton = this.btnClose;
            base.ClientSize = new Size(0x12b, 0x8b);
            base.Controls.Add(this.btnPathBackup);
            base.Controls.Add(this.lblPathBackup);
            base.Controls.Add(this.txtRegisteredFile);
            base.Controls.Add(this.btnClose);
            base.Controls.Add(this.btnOk);
            //base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FormUpdateTaxCode";
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private bool UpdateTaxCode()
        {
            TaxCodeDataAccess access;
            if (!File.Exists(this.txtRegisteredFile.Text))
            {
                MessageBox.Show("Tệp mã số thuế không tồn tại!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
            Microsoft.Office.Interop.Excel.Application application = new ApplicationClass();
            if (application == null)
            {
                MessageBox.Show("Kiểm tra lại Microsoft Excel!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
            System.Data.DataTable table = new System.Data.DataTable();
            DataColumn column = null;
            DataRow row = null;
            column = new DataColumn("Ms_Thue");
            column.DataType = System.Type.GetType("System.String");
            column.MaxLength = 20;
            table.Columns.Add(column);
            column = new DataColumn("Ten_Dvi");
            column.DataType = System.Type.GetType("System.String");
            column.MaxLength = 128;
            table.Columns.Add(column);
            column = new DataColumn("Dia_Chi");
            column.DataType = System.Type.GetType("System.String");
            column.MaxLength = 128;
            table.Columns.Add(column);
            column = new DataColumn("Ngay_Bd");
            column.DataType = System.Type.GetType("System.DateTime");
            table.Columns.Add(column);
            column = new DataColumn("Dai_Dien");
            column.DataType = System.Type.GetType("System.String");
            column.MaxLength = 128;
            table.Columns.Add(column);
            column = new DataColumn("So_CMND");
            column.DataType = System.Type.GetType("System.String");
            column.MaxLength = 20;
            table.Columns.Add(column);
            column = new DataColumn("Ngay_Kt");
            column.DataType = System.Type.GetType("System.DateTime");
            table.Columns.Add(column);
            table.PrimaryKey = new DataColumn[] { table.Columns[0] };
            bool flag = true;
            int num = 1;
            Array array = null;
            object key = DBNull.Value;
            object obj3 = DBNull.Value;
            object obj4 = DBNull.Value;
            object obj5 = DBNull.Value;
            object obj6 = DBNull.Value;
            object obj7 = DBNull.Value;
            object obj8 = DBNull.Value;
            try
            {
                Worksheet worksheet = (Worksheet) application.Workbooks.Open(this.txtRegisteredFile.Text, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing).Sheets.get_Item(1);
                while (flag)
                {
                    num++;
                    array = (Array) worksheet.get_Range("A" + num.ToString(), "G" + num.ToString()).Cells.get_Value(System.Type.Missing);
                    key = array.GetValue(1, 1);
                    obj3 = array.GetValue(1, 2);
                    obj4 = array.GetValue(1, 3);
                    obj5 = array.GetValue(1, 4);
                    obj6 = array.GetValue(1, 5);
                    obj7 = array.GetValue(1, 6);
                    obj8 = array.GetValue(1, 7);
                    if ((key == null) || (obj8 == null))
                    {
                        flag = false;
                        goto Label_0480;
                    }
                    key = key.ToString().Trim();
                    obj5 = string.IsNullOrEmpty(obj5.ToString()) ? ((object) DBNull.Value) : ((object) Convert.ToDateTime(obj5.ToString()));
                    obj8 = string.IsNullOrEmpty(obj8.ToString()) ? ((object) DBNull.Value) : ((object) Convert.ToDateTime(obj8.ToString()));
                    if (string.IsNullOrEmpty(key.ToString()) || obj8.Equals(DBNull.Value))
                    {
                        flag = false;
                        goto Label_0480;
                    }
                    if (table.Rows.Find(key) == null)
                    {
                        row = table.NewRow();
                        row["Ms_Thue"] = key;
                        row["Ten_Dvi"] = obj3;
                        row["Dia_Chi"] = obj4;
                        row["Ngay_Bd"] = obj5;
                        row["Dai_Dien"] = obj6;
                        row["So_CMND"] = obj7;
                        row["Ngay_Kt"] = obj8;
                        table.Rows.Add(row);
                    }
                }
            }
            catch
            {
                if (!string.IsNullOrEmpty(key.ToString()))
                {
                    MessageBox.Show("Hãy kiểm tra lại d\x00f2ng số " + num.ToString() + ".\nMã số thuế đơn vị l\x00e0 " + key.ToString() + "!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    return false;
                }
            }
        Label_0480:
            access = new TaxCodeDataAccess();
            access.TaxCode_Save(table, DateTime.Today);
            return true;
        }
    }
}

